import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class Trinity_MMB(Model3D):
    '''Replace 'Trinity_MMB' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(Trinity_MMB,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_faces(self, meshName):
        
        chunkSize = self.inFile.read_long()
        numFaces = self.inFile.read_long()
        print "%d chunk, %d faces" %(chunkSize, numFaces)
        
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
    
    def parse_vertices(self, meshName):
        
        chunkSize = self.inFile.read_long()
        numVerts = self.inFile.read_long()
        print "%d chunk, %d verts" %(chunkSize, numVerts)
        
        for i in range(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            nx, ny, nz = self.inFile.read_float(3)
            self.inFile.read_long()
            self.inFile.read_float(2)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        meshNum = 0
        meshList = []
        
        filesize = self.inFile.filesize()
        
        chunk = self.inFile.read_long()
        while 1:
            if chunk == 0x01:
                self.inFile.read_long(3)
            elif chunk == 0x02:
                self.inFile.read_long(3)
            elif chunk == 0x03:
                self.inFile.read_long(3)
            elif chunk in [0x05, 0x06, 0x07]:
                chunkType = self.inFile.read_long()
                length = self.inFile.read_long()
                null = self.inFile.read_long()
                name = self.inFile.read_string(length)
                
                if chunkType == 0x3101:
                    meshList.append(name)
                    print name
                elif chunkType == 0x4508:
                    print "material"
                elif chunkType == 0x3101:
                    print "bone"
            elif chunk == 0x08:
                
                meshName = meshList[meshNum]
                
                
                chunkType = self.inFile.read_long()
                if chunkType == 0x4610:
                    print meshName
                    self.create_object(meshName)
                    self.parse_vertices(meshName)
                elif chunkType == 0x4710:
                    self.parse_faces(meshName)
                    meshNum += 1
                else:
                    print "unknown"
            if self.inFile.tell() == filesize:
                break
            chunk = self.inFile.read_long()
            
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = Trinity_MMB(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "MMB", "Trinity Online MMB"

if __name__ == '__main__':
    
    file1 = "WEAPON_BLADE_C01_R1.MMB"
    obj = read_file(file1)